<template>
	<!-- 
		@copyright CYZ
		@time 2022/8/5
		商品列表（公益福利购、免费领、做公益等页面）
	 -->
	<view class="main">
		<!-- :style="Style" -->
		<view v-if="hasFilter" class="tab-box">
			<!-- <text v-if="item.hasIcon" class="iconfont2">&#xe6a7;</text> -->
			<view @click="tabClick(index)" class="tab" :class="tabActiveIndex==index?'active':''"
				v-for="(item,index) in tabList" :key="index">
				<view class="tabView">{{item.text}}</view>
				<image :src="imgUrl+'/career/xuxianLine.png'" class="xuxianLine"></image>

			</view>

		</view>
		<!-- <view v-if="hasFilter" class="split"></view> -->
		<view>
			<view class="list-box">
				<view class="list" v-for="(item,index) in listData" :key="index">
					<view class="item" @click="toDetail(item)">
						<image class="cover" mode="aspectFill" :src="item.cover"></image>
						<view class="content">
							<view class="title-box">
								<image v-if="item.area_feature==2" class="tag" mode="aspectFit"
									src="https://zhongyi-shop.oss-cn-beijing.aliyuncs.com/songhe/weixin/static/common/tag6.png">
								</image>
								<image v-if="item.area_feature==3" class="tag" mode="aspectFit"
									src="https://zhongyi-shop.oss-cn-beijing.aliyuncs.com/songhe/weixin/static/common/tag2.png">
								</image>
								<text class="title u-display1">{{item.title}}</text>
							</view>
							<view class="desc-box">
								<view class="left">
									<template v-if="!item.free">
										<template v-if="!item.point_goods">
											<text class="new-price">
												<text class="company">¥</text>
												<text class="integer">{{formatPrice(item.min_sale_price,'int')}}.</text>
												<text class="float">{{formatPrice(item.min_sale_price,'float')}}</text>
											</text>
											<text class="old-price">¥{{item.min_market_price}}</text>
										</template>
										<template v-else>
											<text class="iconfont2 integral-icon">&#xe669;</text>
											<text class="integral-num">{{item.min_exchange_point}}</text>
										</template>
									</template>
									<template v-if="item.free">
										<image class="tag2" mode="aspectFit"
											src="https://zhongyi-shop.oss-cn-beijing.aliyuncs.com/songhe/weixin/static/common/tag3.png">
										</image>
									</template>
								</view>
								<text v-if="item.free || item.point_goods"
									class="have">{{item.result_sale_num}}件已{{item.free?'申领':'出售'}}</text>
							</view>
						</view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		string
	} from 'mathjs';
	//area_feature 区域类型： 0服务体系礼包 1新人专区 2公益申领专区
	export default {
		props: {
			//是否有过滤条件
			hasFilter: {
				type: Boolean,
				default: true
			},
			//是否有分页
			hasPage: {
				type: Boolean,
				default: true
			},
			listData: {
				type: Array,
				default: () => {
					return []
				}
			},
			paymentPage: {
				type: string,
				default: null
			},
			exchangeType: {
				type: string,
				default: null
			}
		},
		data() {
			return {
				imgUrl: this.$mConfig.staticUrl,
				//手机状态栏高度
				statusBarHeight: uni.getSystemInfoSync().statusBarHeight,
				tabList: [{
					text: '综合',
					hasIcon: false,
					sort: ''
				}, {
					text: '最新',
					hasIcon: false,
					sort: ''
				}, {
					text: '价格',
					hasIcon: true,
					sort: '' // asc或desc
				}, {
					text: '销量',
					hasIcon: true,
					sort: '' // asc或desc
				}],
				tabActiveIndex: 0
			}
		},
		created() {},
		computed: {
			Style() {
				return `height:${this.statusBarHeight}px`;
			}
		},
		methods: {
			/**
			 * @param {Object} type int或float
			 * description 格式化价格
			 */
			formatPrice(price, type) {
				let str = ''
				if (type == 'int') {
					str = String(price).split('.')[0]
				}
				if (type == 'float') {
					str = String(price).split('.').length <= 1 ? '00' : (String(price).split('.')[1])
				}
				return str;
			},
			tabClick(index) {
				let that = this;
				this.tabActiveIndex = index;
				if (index != 0 && index != 1) {
					if (this.tabList[index].sort == '') {
						if (index == 2) {
							this.tabList[index].sort = 'asc';
						} else if (index == 3) {
							this.tabList[index].sort = 'desc';
						}
					} else {
						this.tabList[index].sort = this.tabList[index].sort == 'asc' ? 'desc' : 'asc'
					}
				}
				this.$emit('tabChange', {
					index,
					sort: this.tabList[index].sort
				})
			},
			toDetail(item) {
				uni.navigateTo({
					url: `/pages/product/goods/goods?id=${item.goods_id}&paymentPage=${this.paymentPage}&exchangeType=${this.exchangeType}`
				})
				// 积分商品
				// if(item.point_goods){
				// 	uni.navigateTo({
				// 		url: `/pages/product/goods/IntegralGood?id=${item.goods_id}`
				// 	})
				// // 免费商品
				// }else if(item.free){
				// 	uni.navigateTo({
				// 		url: `/pages/product/goods/freeGoods?id=${item.goods_id}`
				// 	})
				// // 普通商品
				// }else{
				// 	uni.navigateTo({
				// 		url: `/pages/product/goods/goods?id=${item.goods_id}`
				// 	})
				// }
			}
		}
	}
</script>

<style lang="scss" scoped>
	.main {
		.split {
			width: 100%;
			height: 20rpx;
			background-color: #f7f7f7;
		}

		.tab-box {
			position: sticky;
			top: 0;
			left: 0;
			background-color: white;
			z-index: 50;
			height: 86rpx;
			width: 100%;
			display: flex;
			justify-content: space-between;
			align-items: center;
			box-sizing: border-box;
			border-bottom: none;
			height: 85rpx;

			.tab {
				height: 86rpx;
				line-height: 86rpx;
				font-size: 30rpx;
				font-weight: 400;
				text-align: left;
				color: #666666;
				width: 188rpx;
				background: #FFFFFF;
				border-radius: 0rpx 0rpx 0rpx 0rpx;
				display: flex;
				flex-direction: row;
				justify-content: center;
				align-items: center;

				&.active {
					font-weight: 500;
					font-size: 27rpx;
					color: #FF211F;
					line-height: 27rpx;

				}
			}
		}

		.list-box {
			display: flex;
			flex-direction: row;
			flex-wrap: wrap;
			justify-content: space-between;
			box-sizing: border-box;
			padding: 19rpx;

			.list {
				// display: flex;
				// flex-direction: column;
				// // flex: 1;
				// width: 336rpx;

				width: 346rpx;
				height: 471rpx;
				background: #FFFFFF;
				// border-radius: 19rpx 19rpx 19rpx 19rpx;
				margin-bottom: 19rpx;
				border-radius: 40rpx;


				.item {
					background-color: white;
					width: 100%;
					box-sizing: border-box;
					margin-bottom: 30rpx;
					border-radius: 40rpx;
					padding-bottom: 28rpx;
					filter: drop-shadow(1px 2px 4px rgba(26, 58, 70, 0.1));
					height: 471rpx;

					.cover {
						width: 346rpx;
						height: 356rpx;
						background-color: #f7f7f7;
						display: block;
						margin-bottom: 10rpx;
						border-radius: 40rpx 40rpx 0 0;
					}

					.content {
						padding: 0 15rpx;
						height: 115rpx;
						width: 100%;

						.title-box {
							// height: 75rpx;
							overflow: hidden;
							text-overflow: ellipsis;
							display: -webkit-box;
							-webkit-line-clamp: 2;
							-webkit-box-orient: vertical;
							font-weight: 400;
							font-size: 25rpx;
							color: #333333;
							line-height: 27rpx;

							.tag {
								vertical-align: middle;
								margin-right: 15rpx;
								width: 112rpx;
								height: 30rpx;
								line-height: 28rpx;
							}

							.title {
								font-weight: 400;
								font-size: 25rpx;
								color: #333333;
								line-height: 27rpx;
							}
						}

						.tag2 {
							vertical-align: middle;
							margin-right: 15rpx;
							width: 101rpx;
							height: 33rpx;
						}

						.desc-box {
							margin-top: 22rpx;
							display: flex;
							justify-content: space-between;
							align-items: center;

							.left {
								line-height: 24rpx;

								.integral-icon {
									font-size: 50rpx;
									color: #22a736;
									vertical-align: middle;
								}

								.integral-num {
									font-size: 24rpx;
									font-family: PingFang SC, PingFang SC-Medium;
									font-weight: 500;
									text-align: left;
									color: #808080;
									line-height: 24rpx;
								}

								.new-price {
									font-weight: bold;
									font-size: 27rpx;
									color: #FF211F;
									line-height: 27rpx;
									.company {
										font-size: 27rpx;
									}

									.integer {
										font-size: 27rpx;
									}

									.float {
										font-size: 27rpx;
										font-weight: 400;
									}
								}

								.old-price {
									
									font-weight: 400;
									font-size: 23rpx;
									color: #BCBCBC;
									line-height: 17rpx;
									text-decoration: line-through;
									margin-left: 6rpx;
								}
							}

							.have {
								font-size: 24rpx;
								font-family: PingFang SC, PingFang SC-Regular;
								font-weight: 400;
								text-align: left;
								color: #999999;
							}
						}
					}
				}

				&:nth-child(1) {
					margin-right: 9rpx;
				}

				&:nth-child(2) {
					margin-left: 9rpx;
				}
			}
		}
	}

	.xuxianLine {
		height: 27rpx;
		width: 2rpx;
		position: absolute;
		right: 0;
	}

	.tab-box .tab.active::before {
		background-color: #fff;
	}

	.tab-box .tab::before {
		background-color: #fff;
	}
</style>